<template>
  <div>
    <el-card>
      <div slot="header" class="header">
        <el-tabs v-model="info">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visite"></el-tab-pane>
        </el-tabs>
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </div>
      </div>
      <div>
        <el-row :gutter="10">
          <el-col :span="19">
            <div class="charts" ref="charts"></div>
          </el-col>
          <el-col :span="5">
            <h1>待开发....</h1>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from "echarts";
import dayjs from "dayjs";
export default {
  name: "",
  data() {
    return {
      //收集选项卡的数据
      info: "sale",
      //echarts实例属性
      mycharts: "",
      //收集日历的数据
      date: [],
      //将来存储服务器的数据
      chartsList: {},
    };
  },
  mounted() {
    //初始化echarts实例
    this.mycharts = echarts.init(this.$refs.charts);
    //设置配置项
    let option = {
      title: {
        text: "销售额",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [],
          color: "yellowgreen",
        },
      ],
    };
    //设置配置项
    this.mycharts.setOption(option);
    //获取服务器的数据
    this.getChartsData();
  },
  computed: {
    //计算出一个标题：利用info
    title() {
      return this.info == "sale" ? "销售额" : "访问量";
    },
  },
  //监听info的变化
  watch: {
    //监听info变化
    info(newValue) {
      //判断info数据：到底显示那个数组
      if (newValue == "sale") {
        //重新修改配置项
        this.mycharts.setOption({
          series: [
            {
              name: "Direct",
              type: "bar",
              barWidth: "60%",
              data: this.chartsList.data.charts.arr1,
              color: "yellowgreen",
            },
          ],
        });
      }else{
         this.mycharts.setOption({
          series: [
            {
              name: "Direct",
              type: "bar",
              barWidth: "60%",
              data: this.chartsList.data.charts.arr2,
              color: "yellowgreen",
            },
          ],
        });
      }
      //当info发生变化是，再次重新配置图标配置项
      //替换配置项的时候，新的数值会替换旧的数值
      //已有数值没有替换，使用曾经的数据！！！！
      this.mycharts.setOption({
        title: {
          text: this.title,
        },
      });
    },
    //监视chartList数值有没有发生变化
    //如果发生变化服务器数据回来了
    chartsList() {
      //重新修改配置项
      this.mycharts.setOption({
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.chartsList.data.charts.arr1,
            color: "yellowgreen",
          },
        ],
      });
    },
  },
  methods: {
    //今日
    setDay() {
      //获取今日的时间
      const start = dayjs().format("YYYY-MM-DD");
      const end = dayjs().format("YYYY-MM-DD");
      //向数组里面添加数据--两个
      this.date = [start, end];
    },
    //本周
    setWeek() {
      //获取今日的时间
      const start = dayjs().day(1).format("YYYY-MM-DD");
      const end = dayjs().day(7).format("YYYY-MM-DD");
      //向数组里面添加数据--两个
      this.date = [start, end];
    },
    //本月
    setMonth() {
      //获取今日的时间
      const start = dayjs().startOf("month").format("YYYY-MM-DD");
      const end = dayjs().endOf("month").format("YYYY-MM-DD");
      //向数组里面添加数据--两个
      this.date = [start, end];
    },
    //本年
    setYear() {
      //获取今日的时间
      const start = dayjs().startOf("year").format("YYYY-MM-DD");
      const end = dayjs().endOf("year").format("YYYY-MM-DD");
      //向数组里面添加数据--两个
      this.date = [start, end];
    },
    //模拟获取服务器的数据
    getChartsData() {
      //发请求异步
      setTimeout(() => {
        //1S之后服务器数据回来
        this.chartsList = {
          code: 200,
          message: "成功",
          data: {
            charts: {
              arr1: [20, 30, 40, 10, 88, 12, 99],
              arr2: [99, 44, 21, 37, 66, 190, 30],
            },
          },
        };
      }, 1000);
    },
  },
};
</script>

<style scoped lang="scss">
.header {
  position: relative;
  .right {
    position: absolute;
    right: 0px;
    top: 0px;
    span {
      margin: 0px 5px;
    }
  }
}
::v-deep .el-card__header {
  border-bottom: none;
}
.charts {
  width: 100%;
  height: 300px;
}
</style>
